import Util from "../util/util";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Ball extends cc.Component {

    private static readonly ARRAY_BALL_COLOR: cc.Color[] = [
        cc.color(255, 153, 51), 
        cc.color(255, 255, 0), 
        cc.color(51, 102, 153), 
        cc.color(153, 0, 102), 
        cc.color(255, 204, 0), 
        cc.color(204, 0, 51), 
        cc.color(51, 51, 153), 
        cc.color(0, 153, 153)
    ];
    private static readonly INIT_SIZE: number = 30;
    private static readonly BALLGROUP: string = "ball";
    private static readonly DEADBALLGROUP: string = "ballDead";
    private static readonly BALL_RES_PATH: string = "prefebs/Ball";
    private static readonly BOOM_LAST_TIME: number = 1000;

    public static create(onCreated: (ball: Ball) => void, onBallFinish: (Ball)=> void){
        cc.loader.loadRes(this.BALL_RES_PATH, function(err, prefeb){
            if(err){
                cc.log(err || err.message);
                return;
            }
            let ballNode: cc.Node = cc.instantiate(prefeb);
            let ball: Ball = ballNode.getComponent(Ball);
            ball.onBallFinish = onBallFinish;
            onCreated(ball);
        });
    }

    @property(cc.ParticleSystem)
    private particleSystem: cc.ParticleSystem = null;

    private spriteBall: cc.Sprite = null;
    private rigidBody: cc.RigidBody = null;
    private collider: cc.PhysicsCircleCollider = null;
    private audioSource: cc.AudioSource = null;

    private ballNum: number = 1;

    private onBallFinish: (Ball)=> void;

    onLoad(){
        this.rigidBody = this.getComponent(cc.RigidBody);
        this.spriteBall = this.getComponent(cc.Sprite);
        this.collider = this.getComponent(cc.PhysicsCircleCollider);
        this.audioSource = this.getComponent(cc.AudioSource);
    }

    onEnable(){
        this.setNumSizeColor(this.BallNum);
    }

    public init(num: number){
        this.spriteBall.enabled = true;
        this.particleSystem.node.active = false;
        this.node.active = true;
        this.rigidBody.active = false;
        this.setNumSizeColor(num);
    }

    public go(speedX: number, speedY: number){
        Util.changeGroup(this.node, Ball.BALLGROUP);
        this.rigidBody.active = true;
        this.rigidBody.linearVelocity = cc.v2(speedX, speedY);
    }

    private onBeginContact(contact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider){
        let otherBall: Ball = otherCollider.node.getComponent(Ball);
        if(otherBall != null){
            if(this.BallNum == otherBall.BallNum){
                if(this.Speed > otherBall.Speed){
                    otherBall.boomBall();
                    this.setNumSizeColor(this.BallNum + 1);
                }
            }
        }
    }

    private boomBall(){
//        this.node.active = false;

        Util.changeGroup(this.node, Ball.DEADBALLGROUP);
        this.rigidBody.linearVelocity = cc.v2();
        this.spriteBall.enabled = false;
        this.particleSystem.node.active = true;
        this.particleSystem.resetSystem();
        this.audioSource.play();

        let _this = this;
        setTimeout(() => {
            _this.onBallFinish(_this);
        }, Ball.BOOM_LAST_TIME);
    }

    private setNumSizeColor(num: number){
        this.setNum(num);
        this.setSizeByNum(num);
        this.setColorByNum(num);
    }

    private setNum(num: number){
        this.ballNum = num;
    }

    public get BallNum(): number{
        return this.ballNum;
    }

    private setSize(size: number){
        this.node.width = size * 2;
        this.node.height = size * 2;
        this.collider.radius = size;
        this.collider.apply();
    }

    private setSizeByNum(num: number){
//        this.spriteBall.spriteFrame = this.atlasFruit.getSpriteFrame(num.toString());
        this.setSize(Ball.INIT_SIZE * (1 + (num - 1) * 0.5));
    }

    public get Radius(): number{
        return this.collider.radius;
    }

    private setColorByNum(num: number){
        let colorIndex: number = num % Ball.ARRAY_BALL_COLOR.length;
        this.node.color = Ball.ARRAY_BALL_COLOR[colorIndex];
        this.particleSystem.startColor = Ball.ARRAY_BALL_COLOR[colorIndex];
    }

    private get Speed(): number{


        printf("阿天");
        
        System.out.println("阿天");
        console.WriteLine("阿天");
        print("阿天");
        console.log("阿天");
        <p>阿天</p>





        return Math.sqrt(this.rigidBody.linearVelocity.x * this.rigidBody.linearVelocity.x 
            + this.rigidBody.linearVelocity.y * this.rigidBody.linearVelocity.y);
    }

}
                   